<?php
namespace Company\Controller;
use Think\Controller;
class ProcostController extends BaseController {
	public function _initialize()
    {
        parent::_initialize();
        $g=session('agent');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
        $this->product = M('list',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }
    //原料列表
    public function material(){
        $keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$Product=M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		
		$map['goods_cate']=array('neq',1);
        if(!empty($keywords)){
			if($selval=='1'){
				$map['goods_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['goods_name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Product->where($map)->count();
		$list=$Product->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

		$coin_model = M('Coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		foreach($list as $k=>$v){
            $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
            $list[$k]['currency_name'] = $currency['coin_name'];
        }
		
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	/*
     * 编辑入库单、添加商品
     */
    public function edt(){

        $param = I('param.','','trim');
		$Product = M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        if(IS_POST){
			
			$id=trim(I('id'));
			$material_price=trim(I('material_price'));
			$scrap_price=trim(I('scrap_price'));
			
			$map['id']=$id;
			$data['material_price']=$material_price;
			$data['scrap_price']=$scrap_price;
			$status=$Product->where($map)->save($data);
			if($status!==false){
				$this->success('保存成功');
			}else{
				$this->error('保存失败');
			}

        }else{
			$coin_model = M('Coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$id=trim(I('id'));
			$map['id']=$id;
			$p=I('p')?I('p'):1;
			$detail=$Product->where($map)->find();
			$coin_name=$coin_model->where(array('coin_code'=>$detail['current_type']))->find();
			
			$detail['currency_name']=$coin_name['coin_name'];
			$this->assign('detail',$detail);
            $this->display();
        }
    }
	
	//计算入库成本价
	public function count_inbound(){
		$id=trim(I('id'));
		$ProductOrderDetail = M('ProductOrderDetail');
		$ProductOrder = M('ProductOrder');
		$map['plan_id']=$id;
		$maps['id']=$id;
		$list=$ProductOrderDetail->where($map)->select();//取得当前计划单详细信息数据
		//echo $ProductOrderDetail->getlastsql();
		$pro_num=$ProductOrder->where($maps)->find();//取得当前计划单数据
		$num=$pro_num['pro_num'];
		$inbound_price=0;  //入库成本价初始价格
		foreach($list as $k=>$v){
			$count_price=0;		//成本初始价格
			$count_material_price=0; //损耗初始价格
			$count_scrap_price=0; //边角料初始价格
			$count_price=$v['price'];
			$count_material_price+=$v['material_price']*$v['material_weight'];
			$count_scrap_price+=$v['scrap_price']*$v['scrap_weight'];
			$inbound_price+=($count_price-$count_material_price-$count_scrap_price)*$num;//入库成本价=(成本价格-损耗价格-边角料价格)*数量
		}

		$data['status']  = 1;
		$data['info']  = '计算成功';
		$data['inbound_price'] = $inbound_price;
		$this->ajaxReturn($data);
		
	}
	//技术原料生产价
	public function count_material(){
		$id=trim(I('id'));
		$ProductOrderDetail = M('ProductOrderDetail');
		$ProductOrder = M('ProductOrder');
		$map['plan_id']=$id;
		$maps['id']=$id;
		$list=$ProductOrderDetail->where($map)->select();//取得当前计划单详细信息数据
		//echo $ProductOrderDetail->getlastsql();
		$pro_num=$ProductOrder->where($maps)->find();//取得当前计划单数据
		$num=$pro_num['pro_num'];
		$inbound_price=0;  //入库成本价初始价格
		foreach($list as $k=>$v){
			$count_price=0;		//成本初始价格
			$count_material_price=0; //损耗初始价格
			$count_scrap_price=0; //边角料初始价格
			$count_price=$v['price'];
			$count_material_price+=$v['material_price']*$v['material_weight'];
			$count_scrap_price+=$v['scrap_price']*$v['scrap_weight'];
			$inbound_price+=($count_price-$count_material_price-$count_scrap_price)*$num;//入库成本价=(成本价格-损耗价格-边角料价格)*数量
		}

		$data['status']  = 1;
		$data['info']  = '计算成功';
		$data['inbound_price'] = $inbound_price;
		$this->ajaxReturn($data);
		
	}

	//编辑生产计划单
	public function edit_procost(){
		    $param = I('param.','','trim');

			$id=$param['id'];
			
			$ProductOrderDetail = M('ProductOrderDetail');
			$country_model = M('country');
			$ProductOrder=M('ProductOrder');
			$coin_model = M('coin');
            $planPack_model = M('planPack');
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('ProductOrder')->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$this->getconfig();
			$conf=C('PICKCONFIG');
			$listrows=$conf['listrows']?$conf['listrows']:10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['plan_id']=$id;
			$count=$ProductOrderDetail->where($map)->count();
			$list   = $ProductOrderDetail->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			//计算入库成本价-begin
			$maps['id']=$id;
			$listss=$ProductOrderDetail->where($map)->select();//取得当前计划单详细信息数据
			//echo $ProductOrderDetail->getlastsql();
			$pro_num=$ProductOrder->where($maps)->find();//取得当前计划单数据
			$num=$pro_num['pro_num'];
			$inbound_price=0;  //入库成本价初始价格
			foreach($listss as $k=>$v){
				$count_price=0;		//成本初始价格
				$count_material_price=0; //损耗初始价格
				$count_scrap_price=0; //边角料初始价格
				$count_price+=$v['price']*$v['declare_amount'];
				$count_material_price+=$v['material_price']*$v['material_weight'];
				$count_scrap_price+=$v['scrap_price']*$v['scrap_weight'];
				$inbound_price+=($count_price-$count_material_price-$count_scrap_price)*$num;//入库成本价=(成本价格-损耗价格-边角料价格)*数量
				$material_cost_price+=($count_price-$count_scrap_price)*$num;
			}
			//计算入库成本价-end
			
			
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
				$list_price=M('PurchasingInboundGoods')->where(array('goods_no'=>$v['goods_no']))->find();
				$list[$k]['goods_name']=$list_price;
                $list[$k]['currency_name'] = $currency['coin_name'];
				$list[$k]['list_price'] = $list_price;
            }
			
            $data['plan_pack']      = $plan_pack;
			
			$this->assign('inbound_price',$inbound_price);
			$this->assign('material_cost_price',$material_cost_price);
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();
		
	}
	//编辑生产计划单
	public function edit_procost_detail(){
		    $param = I('param.','','trim');
			
            $ProductOrderDetail = M('ProductOrderDetail');
			$ProductOrder = M('ProductOrder');

			$id=$param['id'];
			
			$map['id']=array('neq',$id);
			
           
            //$param['add_time'] = time();

			unset($param['strss']);
            $ress = $ProductOrder->where('id='.$id)->save($param);
			
            if($ress!==false){
				$str=explode(',',I('strss'));
				foreach($str as $k=>$v){
					$arr=explode(':',$v);
					$where['id']=$arr[0];
					$data['material_price']=$arr[1];
					$data['material_weight']=$arr[2];
					
					$data['scrap_price']=$arr[3];
					$data['scrap_weight']=$arr[4];

					$status=$ProductOrderDetail->where($where)->save($data);
					//echo $ProductOrderDetail->getlastsql();

					if($status!==false){
						continue;
					}else{
						$this->error('修改失败');
					}
				}
				
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
		
	}
    //原料采购单信息表列表
	public function planlist(){
        $keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$Product_model=M('ProductOrder');
        if(!empty($keywords)){
			if($selval=='1'){
				$map['purchasing_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['auditor'] = array('like','%'.$keywords.'%');
			}
			$count=$Product_model->where($map)->count();
			$list=$Product_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
        }else{
			$count=$Product_model->count();
			$list=$Product_model->limit($firstrow.','.$listrows)->order('id desc')->select();
		}
		
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	/**
     * 获取配置信息
     */
	function getconfig(){
		  $config =   M('Pickconfig')->getField('title,value');
		  C('PICKCONFIG',$config);
	}
}